/**
 * Created by ebalmes Oct 5, 2009 8:48:13 AM
 * 
 */
package model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import model.types.Patient;

import org.junit.Before;
import org.junit.Test;

import exceptions.DataException;

public class DataModelTest
{
  private DataModel dm = DataModel.getInstance();

  @Before
  public void setUp() throws Exception {
    dm.clearModel();
  }

  @Test
  public void addExistingPatientResultsInException() {
    Patient patient1 = new Patient("test1", 123456789, "anyu1", new Date(
        123456789), "Tata", "Furdo utca 1.");
    Patient patient2 = new Patient("test1", 123456789, "anyu1", new Date(
        123456789), "Tata", "Furdo utca 1.");
    boolean exception = false;
    try {
      dm.addPatient(patient1);
      dm.addPatient(patient2);
    } catch (DataException d) {
      exception = true;
    }
    assertTrue(exception);
  }

  @Test
  public void removePatientDeletesPatient() throws DataException {
    Patient patient1 = new Patient("test1", 123456789, "anyu1", new Date(
        123456789), "Tata", "Furdo utca 1.");
    Patient patient2 = new Patient("test2", 123456782, "anyu2", new Date(
        1234567829), "Tata", "Furdo utca 2.");
    dm.addPatient(patient1);
    dm.addPatient(patient2);
    boolean exception = false;
    try {
      dm.removePatient(patient1.getTajNumber());
      dm.getPatientByTajNumber(patient1.getTajNumber());
    } catch (DataException d) {
      exception = true;
    }
    assertTrue(exception);
  }

  @Test
  public void updateCannotChangeTajNumber() throws DataException {
    Patient patient1 = new Patient("test1", 123456789, "anyu1", new Date(
        123456789), "Tata", "Furdo utca 1.");
    Patient patient2 = new Patient("test2", 123456782, "anyu2", new Date(
        1234567829), "Tata", "Furdo utca 2.");
    dm.addPatient(patient1);
    dm.updatePatient(123456789, patient2);
    Patient patient = dm.getPatientByTajNumber(123456789);
    assertEquals(patient.getName(), patient2.getName());
    assertEquals(patient.getAddress(), patient2.getAddress());
    assertEquals(patient.getDateOfBirth(), patient2.getDateOfBirth());
    assertEquals(patient.getMothersName(), patient2.getMothersName());
    assertEquals(patient.getPlaceOfBirth(), patient2.getPlaceOfBirth());
  }

  @Test
  public void searchPatientsResultsInTheCorrespondingSetOfPatients()
      throws DataException {
    Patient patient1 = new Patient("alma", 123456789, "anyu1", new Date(
        123456789), "Tata", "Furdo utca 1.");
    Patient patient2 = new Patient("korte", 123456782, "anyu2", new Date(
        1234567829), "Tata", "Furdo utca 2.");
    Patient patient3 = new Patient("narancs", 123456779, "anyu1", new Date(
        123456789), "Tata", "Furdo utca 3.");
    Patient patient4 = new Patient("kiwi", 444456782, "anyu2", new Date(
        1234567829), "Tata", "Furdo utca 4.");
    dm.addPatient(patient1);
    dm.addPatient(patient2);
    dm.addPatient(patient3);
    dm.addPatient(patient4);
    List<Patient> expectedList = new ArrayList<Patient>();
    expectedList.add(patient2);
    expectedList.add(patient3);
    List<Patient> resultList = dm.getPatients("r");
    assertEquals(expectedList, resultList);
    
    // if the order of the elements are not the same, the List will not be
    // declared equal
    expectedList.clear();
    expectedList.add(patient1);
    expectedList.add(patient2);
    expectedList.add(patient3);
    resultList = dm.getPatients("123");
    assertEquals(expectedList, resultList);
  }
}
